﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace StatlightTfsBuildIntegration.TfsServiceUtil
{
	public static class ServiceClientFactory
	{
		public static string TfsUrl { get; set; }

		public static IdentityManagementService.IdentityManagementWebServiceSoapClient CreateIdentityManagementWebServiceSoapClient()
		{
			var binding = CreateBinding();
			var endPoint = CreateEndpoint("Services/v3.0/IdentityManagementService.asmx");
			return new IdentityManagementService.IdentityManagementWebServiceSoapClient(binding, endPoint);
		}

		public static TestResultService.TestResultsServiceSoapClient CreateTestResultsServiceSoapClients()
		{
			var binding = CreateBinding();
			var endPoint = CreateEndpoint("TestManagement/v1.0/TestResults.asmx");
			return new TestResultService.TestResultsServiceSoapClient(binding, endPoint);
		}


		private static BasicHttpBinding CreateBinding()
		{
			return new BasicHttpBinding()
					{
						Security = new BasicHttpSecurity
						{
							Mode = BasicHttpSecurityMode.TransportCredentialOnly,
							Message = new BasicHttpMessageSecurity
							{
								AlgorithmSuite = System.ServiceModel.Security.SecurityAlgorithmSuite.Default,
								ClientCredentialType = BasicHttpMessageCredentialType.UserName
							},
							Transport = new HttpTransportSecurity()
							{
								ClientCredentialType = HttpClientCredentialType.Ntlm,
								ProxyCredentialType = HttpProxyCredentialType.None,
							}
						}
					};
		}

		private static EndpointAddress CreateEndpoint(string folder)
		{
			return new EndpointAddress(TfsUrl + "/" + folder);
		}
	}
}
